#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"state.h"

int main(){
	CirList cir;
	pak tpak;
	CirLiNode *t;
	int m, n, *key, i, j;
	cir.node = NULL;
	printf("Input the number of people (n):");
	scanf("%d", &n);
	key = (int *)malloc(n * sizeof(int));
	printf("Input the keys of each people,-1 for randomly generating (key[]):");
	scanf("%d", &key[0]);
	/*Select randomly generating or manual input*/
	if(key[0] == -1){
		srand((unsigned)time(NULL));
		for(i = 0; i < n; i++)
			key[i] = rand()%32767;
	}else
		for(i = 1; i < n; i++)
			scanf("%d", &key[i]);
	printf("Input the initial value of m (m):");
	scanf("%d", &m);
	for(i = 0; i < n; i++){
		tpak.people = i+1;
		tpak.key = key[i];
		addCL(&cir, tpak);
	}
	/*Following two sentence to point the first element of cir*/
	addCL(&cir, tpak);
	delCL(&cir, &tpak);
	/*Output the sequence of joseph*/
	printf("The order is :\n"); 
	for(i = 0; i < n; i++){
		for(j = 1; j < m; j++){
			cir.node = cir.node->next;
		}
		delCL(&cir, &tpak);
		printf("%d\t", tpak.people);
		m = tpak.key;
	}
	printf("\n");
}
